package filesearch

import (
	"fmt"
	"log"
	"os"
	"path/filepath"
	"regexp"
)

// 在目录中搜索文件
func Search(dir, fileNameReg string, recursion bool) (fileNames []string) {
	err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
		fmt.Println(path)

		matched, err := regexp.MatchString(fileNameReg, path)
		if matched {
			if info.IsDir() && recursion {
				fileNames = append(fileNames, Search(filepath.Join(dir, path), fileNameReg, recursion)...)
			} else {
				fileNames = append(fileNames, filepath.Join(dir, path))
			}

		}
		return nil
	})
	if err != nil {
		log.Fatal(err)
	}
	return
}
